import {Request} from "../../utils/request";
import host from "../../utils/host";
import func from "../../utils/functions";
import map from "../../utils/qqmap-wx-jssdk";
const HostUrl = host.service;
const Requ = new Request();
const Func = new func();
const Map = new map({
    key: 'KE4BZ-POLWP-XAHDB-LCLHX-IML6V-E6B7R'
});
var app = getApp();
Page({
  data: {
    // '{"address0":"100000001388","phone0":"13564845736","name0":"比个耶","addname0":"L11栋宿舍三楼307"}'
  },
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight,
    });
    // 获取用户地址
    new Request('get_user_addresslist.cls.php').request(
        () => {
          return {
            data: 1,
            openid: app.globalData.openid,
          }
        },
        (res) => {
            if(res.data.address == undefined){
                Requ.showToast('暂无地址信息');
                return;
            }
            this.setData({
                data: res.data.address
            })
        }
    );
  },
  // 返回上一层
  goback: function (e) {
    setTimeout(function () {
      wx.navigateBack({
        delta: 1
      })
    }, 20)
  },
  add_ad: function () {
    let address = Object;
    Func.GetAddress(this, (res, data)=>{
        address = {
            address0: "2", 
            phone0: data['telNumber'], 
            name0:  data['userName'], 
            addname0:  data['detailInfo'], 
            AddressDetail: data['provinceName']+data['cityName']+data['countyName']+data['detailInfo'],
        };

        let PromiseId = new Promise((resolve, reject)=>{
            // console.log(address.AddressDetail)
            //调用地址解析接口
            Map.geocoder({
                //获取表单传入地址
                address: data['provinceName']+data['cityName']+data['countyName']+data['detailInfo'], 
                //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
                success: (res) => {//成功后的回调
                    resolve(res);
                },
                fail: (res)=>{ // 失败后的回调
                    console.log(res)
                    reject(res);
                }
            });
        }); 
        PromiseId.then((res)=>{
            // console.log(res);
            let AddList = Array();
            AddList.push({'addname': data['detailInfo'],'name': data['userName'],'phone': data['telNumber']});
            console.log(AddList);
            this.setData({
                data: AddList
            });
            address.lat = res.result.location.lat;     // 纬度
            address.lng = res.result.location.lng;    // 经度
            new Request('update_user_address.cls.php').request(
                () => {
                  return {
                    data: 1,
                    openid: app.globalData.openid,
                    address: JSON.stringify(address),
                  }
                },
                (res) => {
					app.globalData.changeAddressFlag = 1
                    res.data.code==1?Requ.showToast('修改成功'):Requ.showToast('修改失败');
                }
            );
        },
        (res)=>{
            Requ.showToast('地址输入有误','error');
        });
        
    });
  },
  // 删除地址
  updelete: function (e) {
      var num = this.data.data.address.length
      console.log(num)
      if(num > 1){
          wx.showModal({
              title: '提示',
              content: '是否删除该地址',
              success: function (res) {
                  if (res.confirm) {
                      wx.showLoading({
                          title: '正在提交',
                          mask: true
                      })
                      wx.request({
                          url: HostUrl.host + 'update_useraddress_del.cls.php',
                          method: 'POST',
                          data: {
                              address: e.currentTarget.dataset.id,
                              openid: app.globalData.openid,
                          },
                          header: {
                              'content-type': 'application/x-www-form-urlencoded',
                              'cookie': 'PHPSESSID=' + app.globalData.phpsessId
                          },
                          success: function (res) {
                              wx.hideLoading()
                              if (res.data.code == -1) {
                                  wx.showToast({
                                      title: '登陆超时',
                                      icon: 'none',
                                      duration: 2000,
                                      mask: true
                                  })

                                  setTimeout(function () {
                                      wx.redirectTo({
                                          url: '/pages/index/index'
                                      })
                                  }, 2000)
                              } else if (res.data.code == 1) {
                                  wx.showToast({
                                      title: '保存成功',
                                      icon: 'success',
                                      duration: 2000,
                                      mask: true
                                  })

                                  setTimeout(function () {
                                      wx.redirectTo({
                                          url: '/pages/manageaddress/manageaddress'
                                      })
                                  }, 2000)
                              } else {
                                  wx.showToast({
                                      title: '保存失败',
                                      icon: 'none',
                                      duration: 2000,
                                      mask: true
                                  })

                                  setTimeout(function () {
                                      wx.redirectTo({
                                          url: '/pages/manageaddress/manageaddress'
                                      })
                                  }, 2000)
                              }
                          }
                      })
                  } else if (res.cancel) {
                      console.log('用户点击取消')
                  }
              }
          })
      }
  }
})